//
//  PrivacyRightsCheck.m
//  Elife
//
//  Created by 拓客营 on 2022/7/20.
//

#import "PrivacyRightsCheck.h"

@implementation PrivacyRightsCheck

//MARK: 检测麦克风权限
+(BOOL)detectionMediaState{
   BOOL isAvalible = NO;

   NSString *mediaType = AVMediaTypeAudio;
   AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];

   //用户已经授权
    if (authStatus == AVAuthorizationStatusAuthorized) {
         isAvalible = YES;
        
    }else if (authStatus == AVAuthorizationStatusNotDetermined) {
        isAvalible = YES;
    }
    //用户拒绝授权
    else {
        //FIXME: 提示用户开启麦克风权限
        [self openSettingPagewithMessage:@"麦克风"];
    }
   return isAvalible;

}

//MARK: 相册访问权限
+(BOOL)detectionPhotoState{
     BOOL isAvalible = NO;

    PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];

     //用户已经授权
    if (authStatus == PHAuthorizationStatusAuthorized) {
        isAvalible = YES;
    }else if(authStatus == PHAuthorizationStatusNotDetermined) {
        isAvalible = YES;
    }
    //用户拒绝授权
    else{
        //FIXME: 提示用户开启相册权限
        [self openSettingPagewithMessage:@"照片"];
    }

      return isAvalible;

}

//MARK: 相机访问权限
+(BOOL)detectionCameraState{
    BOOL isAvalible = NO;

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    
    //用户已经授权
    if (authStatus == AVAuthorizationStatusAuthorized) {
        isAvalible = YES;
    }else if(authStatus == AVAuthorizationStatusNotDetermined) {
        isAvalible = YES;
    }
    //用户拒绝授权
    else {
        //FIXME: 提示用户开启相机权限
        [self openSettingPagewithMessage:@"相机"];
    
    }

    return isAvalible;

}

//MARK: 通知权限
+(BOOL)detectionNotificationState{
    BOOL isAvalible = NO;

    if ([UIApplication sharedApplication].currentUserNotificationSettings.types == UIUserNotificationTypeNone) {
        isAvalible = NO;
    
        //FIXME: 提示用户开启通知权限
//        [SystemManager openSettingPagewithMessage:@"通知"];
    } else {
        isAvalible = YES;
    }


    return isAvalible;
}

//MARK: 定位权限
+(BOOL)detectionLocationState{

    BOOL isAvalible = NO;
    CLAuthorizationStatus  authStatus = [CLLocationManager authorizationStatus];

    if (authStatus == kCLAuthorizationStatusNotDetermined) {
           //检测位置服务是否可用
        if([CLLocationManager locationServicesEnabled]) {
            isAvalible = YES;
       } else {
           //FIXME: 提示用户打开定位权限
           [self openSettingPagewithMessage:@"定位"];
       }
    }
    //用户已经授权
    else if (authStatus == kCLAuthorizationStatusAuthorizedAlways||authStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
        isAvalible = YES;
        
    }
    //用户决绝授权
    else {
        //FIXME: 提示用户打开定位权限
        [self openSettingPagewithMessage:@"定位"];
    }


    return isAvalible;
}

//MARK: 跳转
+(void)openSettingPagewithMessage:(NSString *)message {
    
    [ShowCustomAlertView showCustomAlertWithTitle:[NSString stringWithFormat:@"为了更好的体验功能，请到设置页面->隐私->%@，将对应开关开启",message] leftTitle:@"下次提醒" rightTitle:@"去设置" leftAction:nil rightAction:^{
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    }];
}

@end
